﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hongapo_Common;

public partial class sysadmin_Mall_mallmanager : PageBase
{
    private Hongapo_Model.Product.Product model = new Hongapo_Model.Product.Product();
    private Hongapo_Services.Product.Product bll = new Hongapo_Services.Product.Product();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDowList();
            if (string.IsNullOrEmpty(Request["ID"]))
            {
                this.hiddenOrderNo.Value = "N" + DateTime.Now.ToString("yyMMddHHmmfff");
            }
            else
            {
                LoadText();
            }
            ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
           
        }
    }
    #region 加载商城
    private void LoadDropDowList()
    {
        Hongapo_Services.Mall.Mall bll_mall = new Hongapo_Services.Mall.Mall();
        this.ddlMall.DataSource = bll_mall.GetList(" isopen=1 order by sort");
        this.ddlMall.DataValueField = "ID";
        this.ddlMall.DataTextField = "Title";
        this.ddlMall.DataBind();
    }
    #endregion

    #region 上传产品
    private void Add()
    {
        string orderno = this.hiddenOrderNo.Value;
        try
        {
            model.Clicks =TypeConvert.ToInt32(this.txtClicks.Text);
            model.Createdate = DateTime.Now;
            model.Favours = TypeConvert.ToInt32(this.txtfavours.Text);
            model.Grade = TypeConvert.ToInt32(this.txtGrade.Text);
            model.GradeClicks = TypeConvert.ToInt32(this.txtGradeClicks.Text);
            model.Opposes = TypeConvert.ToInt32(this.txtopposes.Text);
            model.InfoDesc = this.txtInfo.Value;
            model.Isopen = 0;
            model.IsPay = 0;
            model.LinkUrl = this.txtUrlAddress.Text;
            model.MemberID = 1;
            model.MID = TypeConvert.ToInt32(this.ddlMall.SelectedValue);
            model.OldPrice = TypeConvert.ToDecimal(this.txtCostPrice.Text);
            model.OrderNo = this.hiddenOrderNo.Value;
            model.RealPrice = TypeConvert.ToDecimal(this.txtRealPrice.Text);
            model.Remark = "";
            model.Title = this.txtTile.Text;
            model.Summary = StringUtils.CutString(StringUtils.FilterHtml(this.txtInfo.Value), 160);
            
            if (string.IsNullOrEmpty(Request["ID"])) // 新增
            {
                model.IPAdd = objectHandlers.GetIP();
                model.DeliVerName = "后台投递";
                model.DeliVerContact = "后台投递";
                bll.Add(model);
            }
            else // 修改
            {
                model.Isopen = TypeConvert.ToInt32(this.hiddenisoepen.Value, 0);
                model.ID = TypeConvert.ToInt32(Request["ID"]);
                model.IPAdd = this.lblIPAdd.Text;
                model.DeliVerName = this.lbldeliverName.Text;
                model.DeliVerContact = this.lblDelivercontact.Text;
                model.Createdate = TypeConvert.ToDateTime(this.lblCreateDate.Text,DateTime.Now);
                bll.Update(model);
            }
            new Hongapo_Services.Product.ProductImage().UpdateImageIsopen(orderno);// 修改图片为有效
            this.Show("操作成功！", ViewState["UrlReferrer"] as string);
        }
        catch (Exception ex)
        {
            new Hongapo_Services.Product.ProductImage().Delete(orderno);
            throw;
        }
    }
    #endregion
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Add();
    }

    private void LoadText()
    {
        model = bll.GetModel(TypeConvert.ToInt32(Request["ID"]));
        this.hiddenOrderNo.Value = model.OrderNo;
        this.tr_ProcData.Visible = true;
        this.tr_ProcDeliver.Visible = true;
        this.txtCostPrice.Text =((decimal)model.OldPrice).ToString("#0.00");
        this.txtInfo.Value = model.InfoDesc;
        this.txtRealPrice.Text = ((decimal)model.RealPrice).ToString("#0.00");
        this.txtTile.Text = model.Title;
        this.txtUrlAddress.Text = model.LinkUrl;
        this.txtClicks.Text = model.Clicks.ToString();
        this.txtfavours.Text = model.Favours.ToString();
        this.txtGrade.Text = model.Grade.ToString();
        this.txtGradeClicks.Text = model.GradeClicks.ToString();
        this.txtopposes.Text = model.Opposes.ToString();
        this.ddlMall.Items.FindByValue(model.MID.ToString()).Selected = true;
        this.lblCreateDate.Text = model.Createdate.ToString();
        this.lblDelivercontact.Text = model.DeliVerContact;
        this.lbldeliverName.Text = model.DeliVerName;
        this.lblIPAdd.Text = model.IPAdd;
        this.hiddenisoepen.Value = model.Isopen.ToString();
    
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["UrlReferrer"] as string);
    }
}
